/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.univali.programacao.carraid;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author Murilo
 */
public class CarRaid extends JFrame {

    // Teclas
    private boolean leftPressed;
    private boolean rightPressed;
    private boolean shotPressed;
    // Objetos da Tela Principal
    private Pista pista = new Pista();
    private Carro carro = new Carro();
    private ContadorDeFPS fps;
    private List<Tiro> tirosDoCarro = new ArrayList<Tiro>();
    private List<Inimigo> listaDeInimigos = new ArrayList<Inimigo>();
    private ContadorDeFPS contadorDeFPS = new ContadorDeFPS();
    // Recursos Não Visíveis
    private AcumuladorDeTempo temporizadorDeTiros = new AcumuladorDeTempo(150);
    private AcumuladorDeTempo temporizadorDeInimigos = new AcumuladorDeTempo(5000);
    private BufferedImage buffer;

    ////////////////////////////////////////////////////////////////////////////
    public CarRaid() {
        super("Game");
        setSize(300, 600);
        setResizable(false);
        setVisible(true);
        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(new TratadorDeTeclado());
        contadorDeFPS.setX(getWidth() - 100);
        contadorDeFPS.setY(getHeight() - 20);
        carro.setX(getWidth() / 2 - carro.getSprite().getLarguraDosQuadros() / 2);
        carro.setY(getHeight() - carro.getSprite().getAlturaDosQuadros() - 25);
        pista.setX(0);
        pista.setY(0);
    }

    public void executarJogo() {
        boolean gameOver = false;
        try {
            long tempoDoFrame = 0;
            while (!gameOver) {
                long tempoInicial = System.currentTimeMillis();
                tratamentoDeEntradas();
                processa(tempoDoFrame);
                repaint();
                Thread.sleep(10);
                tempoDoFrame = System.currentTimeMillis() - tempoInicial;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void tratamentoDeEntradas() {
        if (leftPressed) {
            carro.setAnguloDaFace(carro.getAnguloDaFace() - 5);
        } else if (rightPressed) {
            carro.setAnguloDaFace(carro.getAnguloDaFace() + 5);
        }

        carro.setIndoParaEsquerda(leftPressed);
        carro.setIndoParaDireita(rightPressed);

        if (shotPressed) {
            disparaTiro();
        }
    }

    private void processa(long tempoDoFrame) {
        pista.update(tempoDoFrame);
        carro.update(tempoDoFrame);
        contadorDeFPS.update(tempoDoFrame);
        temporizadorDeInimigos.update(tempoDoFrame);
        if (temporizadorDeInimigos.expirou()) {
            temporizadorDeInimigos.reseta();
            listaDeInimigos.add(new Inimigo());
        }

        for (Iterator i = listaDeInimigos.iterator(); i.hasNext();) {
            Inimigo inimigo = (Inimigo) i.next();

            if (inimigo.getY() >= 600) {
                i.remove();
            }

            if (inimigo.collidesWith(carro)) {
                i.remove();
            }
            inimigo.update(tempoDoFrame);
        }

    }

    private void disparaTiro() {
    }

    private class TratadorDeTeclado extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_A:
                    leftPressed = true;
                    break;
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_D:
                    rightPressed = true;
                    break;
                case KeyEvent.VK_SPACE:
                    shotPressed = true;
                    break;

            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_A:
                    leftPressed = false;
                    break;
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_D:
                    rightPressed = false;
                    break;
                case KeyEvent.VK_SPACE:
                    shotPressed = false;
                    break;

            }
        }
    }

    @Override
    public void paint(Graphics gr) {
        Graphics2D g = (Graphics2D) buffer.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());

        pista.draw(g);
        carro.draw(g);

        for (int i = 0; i < tirosDoCarro.size(); i++) {
            tirosDoCarro.get(i).draw(g);
        }

        for (int i = 0; i < listaDeInimigos.size(); i++) {
            listaDeInimigos.get(i).draw(g);
        }

        contadorDeFPS.draw(g);

        gr.drawImage(buffer, 0, 0, null);

    }
}
